package juc.blockqueue.aynchronousqueue;

/**
 * @ClassName DirectHandoff
 * @PackageName juc.blockqueue.aynchronousqueue
 * @projectName myThread
 * @Description 1. 线程间直接数据传递
 * @Author games
 * @Date 2025/9/4 下午1:47
 * @Version 1.0
 */
import java.util.concurrent.SynchronousQueue;

public class DirectHandoff {
    public static void main(String[] args) {
        SynchronousQueue<String> queue = new SynchronousQueue<>();

        // 发送线程
        new Thread(() -> {
            try {
                System.out.println("准备发送消息...");
                queue.put("Hello from producer");
                System.out.println("消息已发送");
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }).start();

        // 接收线程
        new Thread(() -> {
            try {
                System.out.println("等待接收消息...");
                String message = queue.take();
                System.out.println("接收到消息: " + message);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }).start();
    }
}

